//
//  AppDelegate.m
//  基本框架
//
//  Created by runone02 on 2018/6/26.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "AppDelegate.h"
#import "MainTabBarController.h"
#import "AppDelegate+Extend.h"

#import "WZXLaunchViewController.h"
#import "HomeWebViewController.h"
#import "HHGuideVeiw.h"
#import "GuideVeiw.h"
#import "WHC_GestureUnlockScreenVC.h"
#import "WSMovieController.h"

UIColor *MainNavBarColor = nil;
UIColor *MainViewColor = nil;
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    
    //广告界面
    //    [self WZXLaunchView];
    [self showVod];
    //    MainTabBarController * rootVC = [[MainTabBarController alloc]init];
    //    rootVC.selectedIndex = 1;
    //    rootVC.view.backgroundColor = [UIColor whiteColor];
    //    self.window.rootViewController = rootVC;
    [self customizeInterface];
    
    //启用数据存储模块
    [AVOSCloud setApplicationId:@"NLO5BWz5VCo0I2yI9qbm2cLN-gzGzoHsz" clientKey:@"B5h8sjxK2tTGnCSiVeeNsols"];
    [AVAnalytics trackAppOpenedWithLaunchOptions:launchOptions];
    
    [AVOSCloud setAllLogsEnabled:YES];
    
    //加载用户sessionToken
    [[ZMUserInfo shareUserInfo] loadUserInfoFromSanbox];
    
    //根据有无SessionToKen去登录
    [self goLogin];
    
    [self.window makeKeyAndVisible];
    // 4. 显示通知引导页
    //    [GuideVeiw show];
    
    return YES;
}

/** 根据ToKen登录 */
- (void)goLogin{
    if ([ZMUserInfo shareUserInfo].sessionToken.length) {
        [AVUser becomeWithSessionTokenInBackground:[ZMUserInfo shareUserInfo].sessionToken block:^(AVUser * _Nullable user, NSError * _Nullable error) {
            if (user) {
                //加载用户信息
                NSLog(@"%@",user);
                [[ZMUserInfo shareUserInfo] loadUserInfo:user];
                
                //发送登录成功信息
                [[NSNotificationCenter defaultCenter]postNotificationName:KLoginStateChangeNotice object:nil];
            }
        }];
    }
}


- (void)WZXLaunchView{
    //广告图片
    NSString *gifImageURL = @"https://goss.veer.com/creative/vcg/veer/800water/veer-153618676.jpg";
    //广告地址
    NSString *url = @"https://nba.hupu.com/";
    ///设置启动页
    [WZXLaunchViewController showWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT- 50) ImageURL:gifImageURL advertisingURL:url timeSecond:5 hideSkip:YES imageLoadGood:^(UIImage *image, NSString *imageURL) {
        
    } clickImage:^(UIViewController *WZXlaunchVC){
        /// 点击广告
        //2.在webview中打开
        HomeWebViewController *VC = [[HomeWebViewController alloc] init];
        VC.urlStr = url;
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:VC];
        [WZXlaunchVC presentViewController:nav animated:YES completion:nil];
        
    } theAdEnds:^{
        //广告展示完成回调,设置window根控制器
        MainTabBarController * rootVC = [[MainTabBarController alloc]init];
        rootVC.selectedIndex = 1;
        rootVC.view.backgroundColor = [UIColor whiteColor];
        self.window.rootViewController = rootVC;
    }];
}

//首次使用APP的视频界面
- (void)showVod{
    NSString *versionCache = [[NSUserDefaults standardUserDefaults] objectForKey:@"VersionCache"];//本地缓存的版本号  第一次启动的时候本地是没有缓存版本号的。
    NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];//当前应用版本号
    
    if (![versionCache isEqualToString:version]){ //如果本地缓存的版本号和当前应用版本号不一样，则是第一次启动（更新版本也算第一次启动）
        WSMovieController *wsCtrl = [[WSMovieController alloc]init];
        wsCtrl.movieURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"qidong"ofType:@"mp4"]];//选择本地的视屏
        self.window.rootViewController = wsCtrl;
        
        //设置上下面这句话，将当前版本缓存到本地，下次对比一样，就不走启动视屏了。
        //也可以将这句话放在WSMovieController.m的进入应用方法里面
        //为了让每次都可以看到启动视屏，这句话先注释掉
        [[NSUserDefaults standardUserDefaults] setObject:version forKey:@"VersionCache"];
    }else{
        //不是首次启动
        //广告展示完成回调,设置window根控制器
        MainTabBarController * rootVC = [[MainTabBarController alloc]init];
        rootVC.selectedIndex = 3;
        rootVC.view.backgroundColor = [UIColor whiteColor];
        self.window.rootViewController = rootVC;
    }
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    [WHC_GestureUnlockScreenVC setUnlockScreen];
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
